package com.tendory.jt808.msg;

import android.util.Log;

import com.tendory.jt808.util.LogUtils;

public abstract class MessageBuilder {

    private static final String TAG = LogUtils.makeTag(MessageBuilder.class);
    public static String phoneStr = "";

    // Optional parameters - initialized to default values
    protected byte cipher = Message.CIPHER_NONE;
    //  protected byte[] phone  = Message.EMPTY_PHONE;
    protected byte[] phone = null;//new byte[]{0x08, (byte) 0x91, 0x23, 0x45, 0x67, (byte) 0x89};
    protected byte[] body = Message.EMPTY_BODY;

    public MessageBuilder() {
        phone(phoneStr);
    }

    public MessageBuilder cipher(byte cipher) {
        switch (cipher) {
            case Message.CIPHER_NONE:
            case Message.CIPHER_RSA:
                this.cipher = cipher;
                break;
            default:
                Log.w(TAG, "cipher: Unknown cipher mode, use default.");
        }

        return this;
    }

    public MessageBuilder phone(String phoneNum) {
        String imei = "00000000".concat(phoneNum);
        imei = imei.substring(imei.length() - 12);
//    phone = new byte[]{0x01, (byte) 0x36,0x23,0x45,0x67, (byte) 0x89};
        byte[] bytes = new byte[imei.length() / 2];
        for (int i = 0; i < imei.length() / 2; i++) {
            String subStr = imei.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        phone = bytes;
//        if (phone != null && phone.length == 6) {
//            this.phone = phone;
//        } else {
//            Log.w(TAG, "phone: Illegal phone number, use default.");
//        }
        return this;
    }

    public abstract Message build();

}
